<?php

namespace app\forms\group;

use yii\db\Exception;
use app\forms\Model;
use app\models\Group;
use yii\web\HttpException;
use app\models\Position;
use app\models\Department;

/**
 * 组织 首页
 */
class IndexForm extends Model
{
    /**
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function index()
    {
        try {
            file_put_contents(\Yii::getAlias('@webroot') . '/test.txt', date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);

            $groupId = \Yii::$app->user->identity->group_id;
            $groupArr = (new Group)->getTree($groupId);
            $position = (new Position)->getTree($groupId);

            $group = [];
            $dept = new Department;
            foreach ($groupArr as $g) {
                $g['isGroup'] = true;
                $g['children'] = $dept->getTree($g['id']);
                $group[] = $g;
            }

            $deptType = Department::getTypeMapping();
            return [
                'group' => $group,
                'position' => $position,
                'deptType' => $deptType
            ];
        } catch (Exception $e) {
            (new self())->serverError();
        }
    }
}